// index.js
const { getCity, getReportTemplate,getCustmorClick,getOpenId} = require("../../api/api");

Page({

  /**
   * 页面的初始数据
   */
  data: {
    topTitle: {
      top: 0,
      height: 0,
    },
    popupShow: false,
    city: '',
    status: '',
    callPhone: '',
    wechatImgUrl: '',
    accessRank:600000,
  },

  showPopup() {
    this.setData({ popupShow: true });
  },

  onPopupClose() {
    this.setData({ popupShow: false });
  },
  goStandard() {
    wx.navigateTo({
      url: '/pages/standard/index',
    })
  },
  goProjectTable(){
    wx.navigateTo({
      url: '/pages/projectTable/index',
    })
  },
  async openReportTemplate() {
    const { data: { url } } = await getReportTemplate()
    if (url) {
      wx.showLoading({ title: "报告加载中..." });
      wx.downloadFile({
        url: url,
        success: (res) => {
          if (res.tempFilePath) {
            wx.openDocument({
              filePath: res.tempFilePath,
              fail: (err) => {
                console.error(err);
              },
              complete: () => {
                wx.hideLoading();
              }
            })
          }
        },
        fail: (err) => {
          console.error(err);
          wx.hideLoading();
        }
      })
    }

  },
  goAppointment() {
    wx.navigateTo({
      url: '/pages/appointment/index',
    })
  },
  goGroupAppointment(){
    wx.navigateTo({
      url: '/pages/groupAppointment/index',
    })
  },
  goOrder() {
    wx.navigateTo({
      url: '/pages/order/index',
    })
  },
  goBasis() {
    wx.navigateTo({
      url: '/pages/basis/index',
    })
  },
  makePhoneCall() {
    wx.makePhoneCall({
      phoneNumber: this.data.callPhone,
    })
  },
  async getInfo() {
    const { data: { city, status, phone, imgUrl } } = await getCity();
    this.setData({
      city,
      status,
      callPhone: phone,
      wechatImgUrl: imgUrl
    })
  },
  //定位方法
  initLocationPersmiss: function () {
    var that = this;
    wx.getSetting({
      success: (res) => {
        // res.authSetting['scope.userLocation'] == undefined  表示 初始化进入该页面
        // res.authSetting['scope.userLocation'] == false  表示 非初始化进入该页面,且未授权
        // res.authSetting['scope.userLocation'] == true  表示 地理位置授权
        if (res.authSetting['scope.userLocation'] != undefined && res.authSetting['scope.userLocation'] != true) {
          //未授权
          wx.showModal({
            title: '请求授权当前位置',
            content: '需要获取您的地理位置，请确认授权',
            success: function (res) {
              if (res.cancel) {
                //取消授权
                wx.showToast({
                  title: '拒绝授权 暂时无法定位当前所在城市',
                  icon: 'none',
                  duration: 1000
                })
              } else if (res.confirm) {
                //确定授权，通过wx.openSetting发起授权请求
                wx.openSetting({
                  success: function (res) {
                    if (res.authSetting["scope.userLocation"] == true) {
                      wx.showToast({
                        title: '授权成功',
                        icon: 'success',
                        duration: 1000
                      })
                      //再次授权，调用wx.getLocation的API
                      that.initGetLocationFlunction();
                    } else {
                      wx.showToast({
                        title: '授权失败',
                        icon: 'none',
                        duration: 1000
                      })
                    }
                  }
                })
              }
            }
          })
        } else if (res.authSetting['scope.userLocation'] == undefined) {
          //用户首次进入页面,调用wx.getLocation的API
          that.initGetLocationFlunction();
        } else {
          console.log('授权成功')
          //调用wx.getLocation的API
          that.initGetLocationFlunction();
        }
      }
    })

  },
  initGetLocationFlunction() {
    let that = this;
    wx.getLocation({
      success(res) {
        console.log('initGetLocationFlunction-success:', res)
        const latitude = res.latitude
        const longitude = res.longitude
        const speed = res.speed
        const accuracy = res.accuracy
      }, fail(res) {
        console.log('initGetLocationFlunction-fail:', res)
      }
    });
  },
  async getClickNum(){
        wx.login({
          success: async (res) => {
            let code = res.code
            const { data: { openid: openId } } = await getOpenId({ code })
            const numResult = await getCustmorClick({
              openId
            })
            console.log("结果")
            console.log(numResult)
            this.setData({
              accessRank: numResult.data.accessRank?numResult.data.accessRank+600000:600000
            })
    
    
          }
        })
      },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    const {
      top,
      height
    } = wx.getMenuButtonBoundingClientRect();
    this.setData({
      topTitle: {
        top,
        height
      }
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // this.getTabBar().init();
    // this.initLocationPersmiss();
    this.getInfo();
    this.getClickNum();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (option) {
    return {
      title: '家晓验房＠品牌验房【全国连锁更专业】',
      path: '/pages/index/index',
      imageUrl: '/assets/image/share-img.png'
    }
  }
})
